package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * TODO: 增加描述
 *
 * @author li.xh
 * @version 1.0.0
 * @date 2019-12-13 12:11
 * @copyright wonhigh.cn
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {


    @Autowired
    private MenuDao menuDao;
    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public List<Menu> findMenuIdsByRoleId(Integer id) {
        return menuDao.findMenuIdsByRoleId(id);
    }

    public void add(Menu menu) {
        menuDao.add(menu);
    }

    //检查项分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//查询条件
        //完成分页查询，基于mybatis框架提供的分页助手插件完成
        PageHelper.startPage(currentPage,pageSize);
        //select * from t_checkitem limit 0,10
        Page<Menu> page  = menuDao.selectByCondition(queryString);
        long            total = page.getTotal();
        List<Menu> rows  = page.getResult();
        return new PageResult(total,rows);
    }

    //根据ID删除检查项
    public void deleteById(Integer id) {
        //判断当前检查项是否已经关联到检查组
        long count = menuDao.findCountByRole(id);
        if(count > 0){
            //当前检查项已经被关联到检查组，不允许删除
            new RuntimeException();
        }
        menuDao.deleteById(id);
    }

    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

}
